﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.TestManagement.Client;

namespace TFSTools.TFSLibrary.TestManagement
{
    public sealed class TFSTestPlan : TFSServerEntity
    {
        private ITestPlan serverTestPlan;
        private TFSTestTeamProject tfsTestTeamProject;

        private TFSTestSuite rootSuite = null;

        internal TFSTestPlan(ITFSConnection connection, ITestPlan testPlan, TFSTestTeamProject project)
            : base(connection)
        {
            this.serverTestPlan = testPlan;
            this.tfsTestTeamProject = project;
        }

        internal ITestPlan ServerEntity { get { return this.serverTestPlan; } }

        public TFSTestTeamProject TestTeamProject { get { return this.tfsTestTeamProject; } }

        public override string ToString() { return this.serverTestPlan.Name; }

        public string Description { get { return this.serverTestPlan.Description; } }

        public DateTime EndDate { get { return this.serverTestPlan.EndDate; } }

        public int Id { get { return this.serverTestPlan.Id; } }

        public string Iteration { get { return this.serverTestPlan.Iteration; } }

        public string Name { get { return this.serverTestPlan.Name; } }

        public DateTime StartDate { get { return this.serverTestPlan.StartDate; } }

        public string State { get { return this.serverTestPlan.State.ToString(); } }

        public string Owner { get { return this.serverTestPlan.Owner.DisplayName; } }

        public TFSTestSuite RootSuite 
        { 
            get 
            {
                if (this.rootSuite == null)
                {
                    this.rootSuite = new TFSTestSuite(this, null, this.serverTestPlan.RootSuite);
                }

                return this.rootSuite; 
            } 
        }
    }

    public sealed class TFSTestPlanCollection : TFSServerEntityCollection<TFSTestPlan>
    {
        private TFSTestTeamProject testTeamProject;

        public TFSTestPlanCollection(TFSTestTeamProject project)
            : base(project.Connection)
        {
            this.ValidateTestTeamProject(project);
        }

        public TFSTestPlanCollection(TFSTestTeamProject project, IList<TFSTestPlan> list)
            : base(project.Connection, list)
        {
            this.ValidateTestTeamProject(project);
        }

        public TFSTestPlanCollection(TFSTestTeamProject project, int capacity)
            : base(project.Connection, capacity)
        {
            this.ValidateTestTeamProject(project);
        }

        private void ValidateTestTeamProject(TFSTestTeamProject project)
        {
            if (project == null)
            {
                throw new InvalidOperationException("TestTeamProject must be specified for TFSTestPlanCollection.");
            }

            this.testTeamProject = project;
        }

        internal static TFSTestPlanCollection Create(TFSTestTeamProject project)
        {
            var plans = project.ServerEntity.TestPlans.Query("select * from TestPlan");

            var list = new List<TFSTestPlan>(plans.Count);

            foreach (ITestPlan testPlan in plans)
            {
                list.Add(new TFSTestPlan(project.Connection, testPlan, project));
            }

            return new TFSTestPlanCollection(project, list);
        }
    }
}
